package api

import (
	"context"
	"server/model"
	"server/model/dao"
	pb "server/proto"
	"server/utils"
)

type VideoServer struct {
	pb.UnimplementedVideoServer
}

func (v *VideoServer) VideoCreate(ctx context.Context, req *pb.VideoCreateRequest) (resp *pb.VideoCreateResponse, err error) {

	video := model.Video{
		UserID:      int(req.Uid),
		Title:       req.Title,
		Icon:        req.Icon,
		Video:       req.Video,
		View:        0,
		Status:      1,
		IsRecommend: 1,
	}
	dao.Create(&video)

	return &pb.VideoCreateResponse{Success: true}, nil
}
func (v *VideoServer) VideoList(ctx context.Context, req *pb.VideoListRequest) (resp *pb.VideoListResponse, err error) {

	pagination := utils.Pagination{
		Page:     int(req.Page),
		PageSize: int(req.PageSize),
	}
	pagination.Validate()
	pagination.Limit()
	pagination.Offset()

	count := dao.GetAllCount()
	totalPage := pagination.SetTotal(count)
	video := dao.GetAllVideo(pagination)
	var result []*pb.Videos

	for _, v := range video {
		result = append(result, &pb.Videos{
			Id:    int32(v.ID),
			Uid:   int32(v.UserID),
			Title: v.Title,
			Icon:  v.Icon,
			Video: v.Video,
		})
	}
	return &pb.VideoListResponse{
		Videos:    result,
		Count:     int32(count),
		TotalPage: int32(totalPage),
	}, nil
}

func (v *VideoServer) UserList(ctx context.Context, req *pb.UserListRequest) (resp *pb.UserListResponse, err error) {

	Video := dao.GetVideoDataByUid(req.Uid)
	var result []*pb.Videos
	for _, v := range Video {
		result = append(result, &pb.Videos{
			Id:    int32(v.ID),
			Uid:   int32(v.UserID),
			Title: v.Title,
			Icon:  v.Icon,
			Video: v.Video,
		})
	}
	return &pb.UserListResponse{Videos: result}, nil
}
